package client.comm.baoding.ui

import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.CateNewAdapter
import client.comm.baoding.adapter.SyPagingHeader2Adapter
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.ActivityTabone2NewBinding
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.comm_ui.draw.SpaceItemBottomDecoration
import client.comm.commlib.getScreenWidth
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.widget.EmptyLayout
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import kotlinx.coroutines.launch

class TabOne2Activity : BaseMiddleActivity<MainViewModel, ActivityTabone2NewBinding>() {

    var cate_id: String = ""

    override fun getLayout(): Int {
        return R.layout.activity_tabone2_new
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val cateNewAdapter: CateNewAdapter by lazy {
        CateNewAdapter(this, mImgLoader).apply {

            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false

                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }

            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }

        }
    }

    override fun initView() {
        intent.getStringExtra("title_name")?.let {
            binding.titleName = it
        }
        intent.getStringExtra("cate_id")?.let {
            cate_id = it
        }
        binding.event = this

        binding.recyclerViewList.apply {
            adapter = cateNewAdapter.withLoadStateFooter(PagerFooterAdapter())
            val gridLayoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            layoutManager = gridLayoutManager
            addItemDecoration(SpaceItemBottomDecoration(10))
        }

        binding.emptyLayout.setOnClickListener {
            cateNewAdapter.refresh()
        }
        binding.refreshLayout.setOnRefreshListener {
            cateNewAdapter.refresh()
        }

    }

    private fun initDataAD(list: List<GoodsIndex.Banner>?) {
        val width = getScreenWidth(this)
        val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
        with(binding) {
            params.height = (width / 2.8f).toInt()//2.25f
            banner.setLayoutParams(params)
            banner.setIndicator(CircleIndicator(this@TabOne2Activity))
        }

        if (list != null) {
            binding.banner.visibility = View.VISIBLE
            list?.let {
                binding.banner.setAdapter(object : SyBannerAdapter<GoodsIndex.Banner>(it) {
                    override fun onBindView(
                        holder: BannerViewHolder?,
                        data: GoodsIndex.Banner?,
                        position: Int,
                        size: Int
                    ) {
                        mImgLoader.load(data?.image_url)
                            .apply(
                                RequestOptions()
                                    .error(R.mipmap.ic_launcher)
                            ).into(
                                holder!!.imageView
                            )
                    }
                })
            }
            binding.banner.start()
        } else {
            binding.banner.visibility = View.GONE
        }

    }

    override fun initData() {
        viewModel.viewModelScope.launch {
            viewModel.pagingDataAuto(20, hold = {

                if (it == 1) {

                    var ret = viewModel.server.goods_type_detail(it, cate_id).body()
                    PagingJsonResult(
                        list = ret?.data?.goods_list,
                        code = ret?.code,
                        message = ret?.message
                    )
                } else {
                    PagingJsonResult(
                        list = arrayListOf(),
                        code = 200,
                        message = ""
                    )
                }

            }).collect {

                cateNewAdapter.submitData(it)
            }
        }

        intent.getParcelableArrayListExtra<GoodsIndex.Banner>("banner")?.let {
            initDataAD(it)
        }

    }

}

